package net.expectx.pay.merchant.controller;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseConstants;
import net.expectx.pay.dao.bean.MerchantOauth;
import net.expectx.pay.dao.bean.MerchantOauthExample;
import net.expectx.pay.dao.bean.MerchantUser;
import net.expectx.pay.merchant.common.AdminConstants;
import net.expectx.pay.rpc.api.MerchantOauthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author lijian
 */
@Controller
@RequestMapping(value = "/manager/merchant/")
public class MerchantController {
    @Autowired
    MerchantOauthService merchantOauthService;
    @ApiOperation(value = "实名认证")
    @RequestMapping(value = "/certification", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String merchantNo=session.getAttribute("merchantNo").toString();
        MerchantUser merchantUser=(MerchantUser) session.getAttribute("merchantUser");
        MerchantOauthExample merchantOauthExample = new MerchantOauthExample();
        MerchantOauthExample.Criteria criteria = merchantOauthExample.createCriteria();
        criteria.andMerchantUserIdEqualTo(merchantUser.getId());
        List<MerchantOauth> merchantOauthList = merchantOauthService.selectByExample(merchantOauthExample);
        if(!merchantOauthList.isEmpty()){
            MerchantOauth merchantOauth = merchantOauthList.get(0);
            modelMap.put("merchantOauth", merchantOauth);
        }

        Map<String,Object>oauthData=merchantOauthService.lookMerchantOauthStatus(merchantUser.getId());
        session.setAttribute("oauthData", oauthData);
        if ((Boolean) oauthData.get("isAuth")){
            return "merchant/certificationAuthDetails";
        }else{
            return "merchant/certificationAuth";
        }

    }
    @ApiOperation(value = "新增认证")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create( MerchantOauth merchantOauth) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MerchantUser merchantUser=(MerchantUser) session.getAttribute("merchantUser");
        merchantOauth.setGmtCreate(System.currentTimeMillis());
        merchantOauth.setGmtModified(System.currentTimeMillis());
        merchantOauth.setIsDeleted(BaseConstants.NO);
        merchantOauth.setMerchantUserId(merchantUser.getId());
        merchantOauth.setStatus(BaseConstants.NO);
        if (null == merchantOauth.getId()) {
            return merchantOauthService.insertSelective(merchantOauth);
        }else{
            return merchantOauthService.updateByPrimaryKeySelective(merchantOauth);
        }
    }
    @ApiOperation(value = "新增认证")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update( MerchantOauth merchantOauth) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MerchantUser merchantUser=(MerchantUser) session.getAttribute("merchantUser");
        merchantOauth.setGmtCreate(System.currentTimeMillis());
        merchantOauth.setGmtModified(System.currentTimeMillis());
        merchantOauth.setIsDeleted(BaseConstants.NO);
        merchantOauth.setMerchantUserId(merchantUser.getId());
        if (null == merchantOauth.getId()) {
            return merchantOauthService.insertSelective(merchantOauth);
        }else{
            return merchantOauthService.updateByPrimaryKeySelective(merchantOauth);
        }
    }
    @ApiOperation(value = "生成业务申请编码")
    @RequestMapping(value = "/code", method = RequestMethod.GET)
    @ResponseBody
    public String code() {
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        Random random = new Random();
            date += random.nextInt(1000);
        return date;
    }

}
